<?php
// +----------------------------------------------------------------------
// | Yzncms [ 御宅男工作室 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://yzncms.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 御宅男 <530765310@qq.com>
// +----------------------------------------------------------------------

// +----------------------------------------------------------------------
// | 后台首页
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\common\controller\Adminbase;
use app\common\model\MemberModel;
use app\common\model\RebateLog;
use think\addons\Service;
use think\Db;
use think\Exception;
use think\facade\Cache;
use think\facade\Request;

class Today extends Adminbase
{
    protected $noNeedLogin = ['login', 'logout'];
    protected $noNeedRight = ['index', 'cache'];

    //初始化
    protected function initialize()
    {
        parent::initialize();
        //移除HTML标签
        $this->request->filter('trim,strip_tags,htmlspecialchars');
    }

    //后台首页
    public function index()
    {
        if ($this->request->post()) {
            $params = $this->request->post();
            if (!$params['price'] || !$params['date']) {
                $this->error("缺少参数！");
            }

            try {
                Db::startTrans();
                $rebateStatus = RebateLog::where(['day' => $params['date']])->find();

                if (!RebateLog::where(['day' => $params['date']])->update(['money' => $params['price']])) {
                    throw new Exception('操作失败，请重新尝试');
                }

                //更新个人收益
                $rebateLog = RebateLog::where(['day' => $params['date']])
                    ->field('day, SUM(money) as total_money,uid,count(id) as count')
                    ->group('uid')
                    ->select();

                foreach ($rebateLog as $key => $value) {
                    if (!(new MemberModel())->where(['id' => $value['uid']])->find()) {
                        continue;
                    }
                    if ($rebateStatus['money']) {
                        (new MemberModel())->where(['id' => $value['uid']])->setDec('total_count', $value['count'] * $rebateStatus['money']);
                    }

                    if (!(new MemberModel())->where(['id' => $value['uid']])->setInc('total_count', $value['total_money'])) {
                        throw new Exception('添加失败，请重新尝试');
                    }
                }
                Db::commit();
                $result = ["code" => 1, 'msg' => '操作成功'];
                return json($result);
            } catch (\Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }

        }
        return $this->fetch();
    }


}
